package com.nm.orm.dao.impl;

import com.nm.commons.exception.SupperException;
import com.nm.orm.model.State;
import com.nm.orm.dao.CategoryDao;
import com.nm.orm.pojo.CategoryEntity;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 功能说明：分类管理dao
 * @return <br/>
 *         修改历史：<br/>
 *         1.[2017年02月12日上午14:57] 创建方法 by hw
 */
@Repository
@Qualifier("categorysDao")
public class CategoryDaoImpl extends DaoImpl<CategoryEntity, String>
        implements CategoryDao {

    private CategoryDaoImpl() {
        super(CategoryEntity.class);
    }

    private static final String UPDATE_AT = "updatedAt";

    private static String STATUS = "state";

    private static String ORDER = "orderBy";

    public List<CategoryEntity> findAll() throws SupperException {
        Criteria criteria = getCurrentSession(sessionFactory)
                .createCriteria(CategoryEntity.class)
                .addOrder(Order.desc(ORDER))
                .add(Restrictions.eq(STATUS, State.ABLE.dbValue()));

        return criteria.list();
    }
}
